package com.example.photo;

import java.util.Random;

import com.example.startscreen.BitmapPositioningActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements Runnable {
	// kalibraciona relacija
	int currentY = 0;
	int currentX = 0;
	public int offset = 0;
	//

	Thread thread = null;
	SurfaceHolder surfaceHolder;
	volatile boolean running = false;
	private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	Random random;
	int i = 1;
	int j = 1;
	public int verticalCalibration; // pitch
	public int horyzontalCalibration; // azymuth

	public MySurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		surfaceHolder = this.getHolder();
		// getHolder().addCallback(this);
		random = new Random();
	}

	public void setHolder() {
		surfaceHolder = getHolder();
		random = new Random();
	}

	public void onResumeMySurfaceView() {
		running = true;
		thread = new Thread(this);
		thread.start();
	}

	public void onPauseMySurfaceView() {
		boolean retry = true;
		running = false;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void run() {
		// TODO Auto-generated method stub
		surfaceHolder = this.getHolder();
		while (running) {
			if (surfaceHolder.getSurface().isValid()) {
				// Canvas canvas = surfaceHolder.lockCanvas();
				// //... actual drawing on canvas
				// paint.setStyle(Paint.Style.STROKE);
				// paint.setStrokeWidth(3);
				// int w = canvas.getWidth();
				// int h = canvas.getHeight();
				// int x = random.nextInt(w - 1);
				// int y = random.nextInt(h - 1);
				// int r = random.nextInt(255);
				// int g = random.nextInt(255);
				// int b = random.nextInt(255);
				// paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
				// canvas.drawPoint(x, y, paint);
				// surfaceHolder.unlockCanvasAndPost(canvas);

				Canvas canvas = surfaceHolder.lockCanvas();

				// int width = bm.getWidth();
				// int height = bm.getHeight();
				// float scaleWidth = ((float) width * 2) / width;
				// float scaleHeight = ((float) height * 2) / height;
				// // create a matrix for the manipulation
				// Matrix matrix = new Matrix();
				// // resize the bit map
				// matrix.postScale(scaleWidth, scaleHeight);
				// // recreate the new Bitmap
				// Bitmap resizedBitmap = b.createScaledBitmap(b, i, j, true);

				// za obican scroll ka dolje
				// Bitmap resizedBitmap =
				// BitmapPositioningActivity.b.createBitmap(BitmapPositioningActivity.b,
				// 0, i, 400, 480);
				// i++;
				// j++;
				// if (i+480 >= BitmapPositioningActivity.b.getHeight()/* ||
				// j>=b.getWidth()*/) {
				// i = 1;
				// j = 1;
				// }
				// canvas.drawBitmap(resizedBitmap, 0, 0, null);
				// surfaceHolder.unlockCanvasAndPost(canvas);
				//

				// KALIBRACIONA RELACIJA

				if (this.verticalCalibration < 131) {
					this.currentY = (int) (((BitmapPositioningActivity.b
							.getHeight() / 180) - 0.1) * this.verticalCalibration);

					// changed code00
					this.currentX = (int) (((BitmapPositioningActivity.b
							.getWidth() / 360) - 0.1) * this.horyzontalCalibration);
					// end changed code00

					// za ZTE
//					Bitmap resizedBitmap = Bitmap.createBitmap(
//							BitmapPositioningActivity.b, this.currentX,
//							this.currentY, 450, 480);
					
					Bitmap resizedBitmap = Bitmap.createBitmap(
							BitmapPositioningActivity.b, this.currentX,
							this.currentY, 450, 480);
					

					i++;
					j++;
					// if (i+480 >= BitmapPositioningActivity.b.getHeight()/* ||
					// j>=b.getWidth()*/) {
					// i = 1;
					// j = 1;
					// }

					canvas.drawBitmap(resizedBitmap, 0, 0, null);

					surfaceHolder.unlockCanvasAndPost(canvas);

				} else {
					// canvas.drawBitmap(BitmapPositioningActivity.b, 0, 0,
					// null);
					this.verticalCalibration = 131;
					this.currentY = (int) (((BitmapPositioningActivity.b
							.getHeight() / 180) - 0.01) * this.verticalCalibration);

					Bitmap resizedBitmap = Bitmap
							.createBitmap(BitmapPositioningActivity.b, 0,
									this.currentY, 450, 480);
					canvas.drawBitmap(resizedBitmap, 0, 0, null);

					surfaceHolder.unlockCanvasAndPost(canvas);
				}

			}
		}
	}

}
